import { PrismaClient } from "@prisma/client"
import { registerDecorator, ValidationOptions } from "class-validator"

// 验证表字段是否不存在
export function IsNotExistsRule(
  table:string,
  validationOptions?: ValidationOptions){
  return function(object:Record<string, any>, propertyName: string) {
 
      registerDecorator({
        name: 'isNotExists',
        target: object.constructor,
        propertyName: propertyName,
        constraints: [table],
        options: validationOptions,
        validator: {
          async validate(value: any, args: any) {
           const prisma = new PrismaClient()
           const res = await prisma[table]?.findUnique({
             where: {
               [propertyName]: value,
             },
          },
          )
           return !Boolean(res)
        },
      }
      })
  }
}